package club.gskj.gulimall.order.web;

import club.gskj.common.constant.AuthServerConstant;
import club.gskj.common.exception.NoStockException;
import club.gskj.common.vo.MemberRespVo;
import club.gskj.gulimall.order.entity.OrderEntity;
import club.gskj.gulimall.order.feign.CartFeignService;
import club.gskj.gulimall.order.feign.MemberFeignService;
import club.gskj.gulimall.order.interceptor.UserLoginInterceptor;
import club.gskj.gulimall.order.service.OrderService;
import club.gskj.gulimall.order.vo.MemberAddressVo;
import club.gskj.gulimall.order.vo.OrderConfirmVo;
import club.gskj.gulimall.order.vo.OrderSubmitVo;
import club.gskj.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import sun.plugin2.message.Message;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /***
     *  测试死信 MQ
     */
    @GetMapping("/test/createOrder")
    @ResponseBody
    public String testCreateOrder() {
        OrderEntity orderEntity = new OrderEntity();

        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());
        // 给MQ发送消息
        rabbitTemplate.convertAndSend("order-event-exchange", "order-create-order", orderEntity);
        return "success";
    }

    /***
     *  跳转到 订单信息确认页面
     */
    @GetMapping("/toTrade")
    public String toTrade(HttpServletRequest request, Model model) throws ExecutionException, InterruptedException {

        OrderConfirmVo confirmVO = orderService.confirmOrder();

        model.addAttribute("orderConfirm", confirmVO);
        return "confirm";
    }

    /***
     * 下单功能
     * @param vo 订单信息
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model, RedirectAttributes attributes) {

        // 下单：创建订单、验令牌、锁库存
        // 下单成功，跳转订单支付页
        // 下单失败，重新跳转确认订单页面

        SubmitOrderResponseVo responseVo = new SubmitOrderResponseVo();
        try {
            responseVo = orderService.submitOrder(vo);
            // 创建订单成功
            if (responseVo.getCode() == 0) {
                // 创建订单成功返回 支付页面
                model.addAttribute("responseVo", responseVo);
                return "pay";
            }
            String msg = "下单失败: ";

            switch (responseVo.getCode()) {
                case 1:
                    msg += "商品订单信息失效，请重新提交商品订单";
                    break;
                case 2:
                    msg += "商品价格变化发生变动，请确认后再次提交商品订单";
                    break;
            }
            // 下单失败，跳转订单确认页
            attributes.addFlashAttribute("errprMsg", msg);
            return "redirect:http://order.gulimall.com/toTrade";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof NoStockException) {
                String message = ((NoStockException) e).getMessage();
                attributes.addFlashAttribute("errprMsg", message);
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
